package com.service.websocket.to;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.service.websocket.handler.AbstractMessageHandler;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author xianggj
 * @description: 与前端交互传递对象
 * @date 2019/8/20 17:53
 */
@Data
@NoArgsConstructor
public class MessageTO  {

    public MessageTO(String topic,  Object data, String windowId) {
        this.topic = topic;
        this.data = data;
        this.time = System.currentTimeMillis();
        this.windowId = windowId;
    }

    /**
     * 返回状态
     */
    public static final String SUCCESS = "0";
    public static final String FAILD = "1";
    /**
     * 返回的类型 topic 参数值参考{@link Topic}
     */
    private String topic ;

    /**
     * 耗时
     */
    private long time;

    /**
     * 当前窗口id
     */
    private String windowId;

    /**
     * 用户id
     */
    private transient String userId;
    /**
     * 用户名称 用于传递给前端显示
     */
    private transient String userName;

    /**
     * 业务id
     */
    private transient String busId;

    /**
     * token
     */
    private transient String token;

    /**
     * 业务id
     */
    private transient String header;

    /**
     * 返回目标 参数值参考{@link ReturnTarget}
     * 目标：所有人 当前窗口 当前用户
     */
    private transient String returnTarget;


    /**
     * 数据信息 传递来时是参数 ,传递回去是结果
     */
    private Object data = new Object();


    /**
     * 业务对象
     */
    private transient AbstractMessageHandler amp;

    public void setWindowId(String windowId) {
        if (StrUtil.isBlank(windowId)){
            windowId = IdUtil.fastSimpleUUID();
        }
        this.windowId = windowId;
    }


    /**
     * 默认值为usr
     */
    public String getReturnTarget() {
        if (StrUtil.isBlank(returnTarget)){
            return ReturnTarget.ALL.getValue();
        }
        return returnTarget;
    }

}
